/******************************************************************************************
* Project: Workhorses of Opportunity — Howard and Weinstein
*
*
* Input:
*   - "tfs_cleaned.dta"
*
* Output:
*   - compliers_high_vs_low.pdf
*   - frictions_high_vs_low.pdf
******************************************************************************************/
clear all

use "tfs_cleaned", clear

* Generate high-income dummy
gen age20orover = ageUSE >= 20
replace age20orover = . if ageUSE == .
gen sattothun = sattot / 1000
replace ageUSE = ageUSE / 10
bys year: egen medianlogincome = median(lninc_midpoint)
gen highincome = lninc_midpoint > medianlogincome
drop if lninc_midpoint == .

* Variables of interest
local vars c_visits c_parentss c_teachers c_prvtcounss c_notafffirsts c_costs c_lowtuits f_jobs f_ftjobs a_fam_c a_loan_c r_awayhomes r_jobs r_nojobs c_goodjobss c_acadreputs c_topgrads r_cultures c_earlys r_findpurps numapplyone sattothun rate01 f_transfs firstchoice fatheduc2 fatheduc3 fatheduc5 fatheduc6 fatheduc8 motheduc2 motheduc3 motheduc5 motheduc6 motheduc8 white lninc_midpoint planlivefam c_nearhomes r_friendsgoings c_friendsatts ageUSE c_onlines c_friendsuggs c_liveathomes

keep `vars' home_hasnormalschool homestate homectyfips year studwgt highincome

* Loop through variables and run regressions by income group
timer clear
foreach x in `vars' {
	disp "`x'"
	timer on 1
	qui {
		preserve
			keep `x' home_hasnormalschool homestate homectyfips studwgt highincome
			collapse `x' [w=studwgt], by(highincome home_hasnormalschool homectyfips homestate)
			reghdfe `x' home_hasnormalschool if highincome, absorb(homestate) cluster(homestate)
			estimates store `x'_high
			reghdfe `x' home_hasnormalschool if !highincome, absorb(homestate) cluster(homestate)
			estimates store `x'_low
		restore
	}
	timer off 1
	timer list 1
}

* Define variable groups
local compliers c_earlys_high c_earlys_low numapplyone_high numapplyone_low sattothun_high sattothun_low ageUSE_high ageUSE_low white_high white_low lninc_midpoint_high lninc_midpoint_low motheduc3_high motheduc3_low motheduc8_high motheduc8_low r_nojobs_high r_nojobs_low r_jobs_high r_jobs_low c_goodjobss_high c_goodjobss_low c_acadreputs_high c_acadreputs_low r_cultures_high r_cultures_low r_findpurps_high r_findpurps_low
local frictions c_notafffirsts_high c_notafffirsts_low f_jobs_high f_jobs_low f_ftjobs_high f_ftjobs_low c_liveathomes_high c_liveathomes_low planlivefam_high planlivefam_low a_fam_c_high a_fam_c_low a_loan_c_high a_loan_c_low f_transfs_high f_transfs_low c_onlines_high c_onlines_low c_visits_high c_visits_low c_parentss_high c_parentss_low c_teachers_high c_teachers_low c_prvtcounss_high c_prvtcounss_low c_friendsuggs_high c_friendsuggs_low c_nearhomes_high c_nearhomes_low r_friendsgoings_high r_friendsgoings_low c_friendsatts_high c_friendsatts_low

* Export estimates for plotting
tempfile mystuff
local counter = 0
foreach x in `compliers' `frictions' {
	local counter = `counter' + 1
	preserve
		est restore `x'
		parmest, fast
		gen outcome = "`x'"
		gen counter = `counter'
		cap append using `mystuff'
		save `mystuff', replace
	restore
}
use `mystuff', clear

* Clean up for plotting
gen high = substr(outcome, -4, 4) == "high"
drop if parm == "_cons"
replace counter = counter + 0.25 if high
replace counter = counter - 0.25 if !high
gen min90 = estimate - 1.68 * stderr
gen max90 = estimate + 1.68 * stderr

* Plot: Compliers (counter <= 28)
scatter counter estimate if high & counter <= 28 || ///
	rspike min95 max95 counter if high & counter <= 28, horizontal color(dkgreen) || ///
	rcap min90 max90 counter if high & counter <= 28, horizontal color(dkgreen) || ///
	scatter counter estimate if !high & counter <= 28, color(orange_red) msym(Sh) || ///
	rspike min95 max95 counter if !high & counter <= 28, horizontal color(orange_red) || ///
	rcap min90 max90 counter if !high & counter <= 28, horizontal color(orange_red) ///
	legend(label(1 "High-income") label(4 "Low-income") order(1 4)) xline(0, lpattern(dash) lcolor(gs8)) ytitle("") ///
	ylabel(1.5 "Chose College Because Applied Early" ///
	3.5 "Applied to One College" ///
	5.5 "SAT score (1000s)" ///
	7.5 "Age (10s)" ///
	9.5 "Race: White" ///
	11.5 "Log parent income" ///
	13.5 "Mom educ: HS" ///
	15.5 "Mom educ: Grad. sch." ///
	17.5 "Going to college because no job" ///
	19.5 "Going to college for better job" ///
	21.5 "Chose college because of graduates' jobs" ///
	23.5 "Chose college because academic reputation" ///
	25.5 "Going to college to become cultured" ///
	27.5 "Going to college to find purpose", angle(0)) ///
	yscale(rev)

graph export "compliers_high_vs_low.pdf", as(pdf) replace

* Plot: Frictions (counter > 28)
scatter counter estimate if high & counter > 28 || ///
	rspike min95 max95 counter if high & counter > 28, horizontal color(dkgreen) || ///
	rcap min90 max90 counter if high & counter > 28, horizontal color(dkgreen) || ///
	scatter counter estimate if !high & counter > 28, color(orange_red) msym(Sh) || ///
	rspike min95 max95 counter if !high & counter > 28, horizontal color(orange_red) || ///
	rcap min90 max90 counter if !high & counter > 28, horizontal color(orange_red) ///
	legend(label(1 "High-income") label(4 "Low-income") order(1 4)) xline(0, lpattern(dash) lcolor(gs8)) ytitle("") ///
	ylabel(29.5 "Chose college because could not afford 1st choice" ///
	31.5 "Likely to have job to pay for college" ///
	33.5 "Likely to have full-time job in college" ///
	35.5 "Chose college because wanted to live at home" ///
	37.5 "Plan on living with family freshman year" ///
	39.5 "Financing college via family and friends" ///
	41.5 "Financing college via loans" ///
	43.5 "Likely to transfer" ///
	45.5 "Chose college because of online ed." ///
	47.5 "Chose college because of visit" ///
	49.5 "Chose college because of parents" ///
	51.5 "Chose college because of teacher" ///
	53.5 "Chose college because of private counselor" ///
	55.5 "Chose college because of friend" ///
	57.5 "Chose college because wanted to live near home" ///
	59.5 "Going to college because friends going" ///
	61.5 "Chose college because friends attending", angle(0)) ///
	yscale(rev)

graph export "frictions_high_vs_low.pdf", as(pdf) replace
